<?php
    include("./include.php");
	
	if(!$_SESSION[user_id]){
		?>
		<script>
			alert("로그인이 필요합니다");
			location.replace("./board_login.php");
		</script>
		<?
	}
	
	$sql = "select * from tb_board where b_idx = '".$_POST[b_idx]."'";
	$result = sql_querry($sql);
	$data = mysql_fetch_array($result);
	
	if(!$data[b_idx]){
		?>
		<script>
			alert("존재하지 않는 글입니다");
			history.back();
		</script>
		<?
	}
	
	if(strlen($data[b_reply]) == 3){
		?>
		<script>
			alert("더 이상 댓글을 쓸 수 없습니다");
			history.back();
		</script>
		<?
	}
	
	$sql2 = "select * from tb_board where b_num ='".$data[b_num]."' and b_reply like '".$data[b_reply]."%' order by b_reply desc limit 1";
	$result2 = sql_querry($sql2);
	$data2 = mysql_fetch_array($result2);
	
	$last_reply_char = substr($data2[b_reply], strlen($data[b_reply]), 1);
	if($last_reply_char == "Z"){
		?>
		<script>
			alert("더 이상 댓글을 쓸 수 없습니다");
			history.back();
		</script>
		<?
	}
	
	if(trim($_POST[b_title]) == ""){
		?>
		<script>
        alert("글제목을 입력해 주세요.");
        history.back();
    	</script>
    	<?
    	exit;
	}
	
	if(trim($_POST[b_contents]) == ""){
		?>
		<script>
        alert("글내용을 입력해 주세요.");
        history.back();
    	</script>
    	<?
    	exit;
	}
	
	$b_num = $data[b_num];
	if($last_reply_char){
		$b_reply = $data[b_reply].chr(ord($last_reply_char) + 1);
	}else{
		$b_reply = $data[b_reply]."A";
	}
	
	$sql = "insert into tb_board set b_num ='".$b_num."', b_reply = '".$b_reply."', m_id = '".$_SESSION[user_id]."', 
			m_name = '".$_SESSION[user_name]."', b_title = '".addslashes(htmlspecialchars($_POST[b_title]))."', b_contents = '"
			.addslashes(htmlspecialchars($_POST[b_contents]))."', b_regdate = now()";
	sql_querry($sql);
?>
<script>
	alert("글이 저장 되었습니다.");
	location.replace("./board_list.php");
</script>